package com.koron.rating.change;

import cn.hutool.core.collection.CollUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.FastJsonUtils;
import com.koron.common.core.util.StringUtils;
import com.koron.rating.change.bean.UnitChangeBean;
import com.koron.rating.change.mapper.UnitChangeDetBeanMapper;
import com.koron.rating.change.mapper.UnitEquipmentDetBeanMapper;
import com.koron.rating.units.RatingUnits;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: rocky
 * @Date: 2022/1/11 9:46
 */
@Slf4j
@RequestMapping("/ratingchange/workflow")
@RestController
public class RatingChangeWorkflowController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }


    @Autowired
    private SystemFeignService systemfeignservice;//获取审批人 公共接口


    @PostMapping("/draftwrite/submitter")
    public Response<List<WorkflowUser>> draftwrite(@RequestBody SubmitterParam submitterParam) {
        //选择 设备管理单位负责人审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 1);
        return Response.success(list);
    }

    @PostMapping("/callunit/submitter")
    public Response<List<WorkflowUser>> callunit(@RequestBody SubmitterParam submitterParam) {
        //选择 设备管理部门分管领导审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 2);
        return Response.success(list);
    }

    @PostMapping("/deptleader/submitter")
    public Response<List<WorkflowUser>> deptleader(@RequestBody SubmitterParam submitterParam) {
        //选择 设备管理部门负责人审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 3);
        return Response.success(list);
    }

    @PostMapping("/deptfzr/submitter")
    public Response<List<WorkflowUser>> deptfzr(@RequestBody SubmitterParam submitterParam) {
        //选择 技术部负责人审批
        List<WorkflowUser> list = getWorkflowUsers(submitterParam, 4);
        return Response.success(list);
    }


    /**
     * 获取下一机电审批人
     *
     * @param submitterParam
     * @param type
     * @return
     */
    public List<WorkflowUser> getWorkflowUsers(SubmitterParam submitterParam, Integer type) {
        List<WorkflowUser> list = new ArrayList<>();
        UnitChangeBean change = FastJsonUtils.getJsonToBean(submitterParam.getFormVariables(), UnitChangeBean.class);
        log.info("接收到的参数:" + FastJsonUtils.getBeanToJson(submitterParam));
        // 找出变更设备的管理单位
        List<String> depts = ADOConnection.runTask(getCurrDs(), factory -> {
            UnitEquipmentDetBeanMapper mapper = factory.getMapper(UnitEquipmentDetBeanMapper.class);
            UnitChangeDetBeanMapper detmapper = factory.getMapper(UnitChangeDetBeanMapper.class);
            List<String> changeDeptIds = mapper.queryDeptIdByChangeId(change.getId());//先找到变更树的单位ids
            List<String> temp = detmapper.queryEquipmentIdByChangeIdAndIdIn(change.getId(), changeDeptIds);//原设备管理单位id
            return temp;
        }, List.class);

        // 获取配置的岗位id
        List<String> postid = RatingUnits.getPostId(submitterParam);//岗位在业务配置设置

        //1选择 设备管理单位负责人审批
        if (type == 1) {
            PostUserQuery postUserQuery = new PostUserQuery();
            postUserQuery.setPostIdList(postid);
            postUserQuery.setOrgIdList(depts);
            List<PostUserVo> pulist = RatingUnits.getWorkflowUsers(postUserQuery);
            list = reduceUser(pulist);
        }

        //2选择 设备管理部门分管领导审批 部门分管副总（机电）
        //3选择 设备管理部门负责人审批 部门总经理
        if (type == 2 || type == 3) {
            //获取配置的岗位id
            PostUserQuery postUserQuery = new PostUserQuery();
            postUserQuery.setPostIdList(postid);
            String deptId = RatingUnits.queryDeptByOrgId(depts.get(0));//依据管理单位找出上级部门
            postUserQuery.setParentOrgIdList(CollUtil.newArrayList(deptId));
            List<PostUserVo> pulist = RatingUnits.getWorkflowUsers(postUserQuery);
            list = reduceUser(pulist);
        }

        //4选择 生产技术部专业管理审批 部门总经理
        if (type == 4) {
            List<String> deptId = RatingUnits.getDeptId(submitterParam);//指定部门在业务配置设置
            PostUserQuery postUserQuery = new PostUserQuery();
            postUserQuery.setPostIdList(postid);
            if (StringUtils.isNotEmpty(deptId)) {
                postUserQuery.setParentOrgIdList(deptId);//
            }
            List<PostUserVo> pulist = RatingUnits.getWorkflowUsers(postUserQuery);
            list = reduceUser(pulist);
        }

        log.info("返回人员:" + FastJsonUtils.getBeanToJson(list));
        return list;
    }

    /**
     * 归集人员
     *
     * @param ulist
     */
    private List<WorkflowUser> reduceUser(List<PostUserVo> ulist) {
        if (StringUtils.isEmpty(ulist)) {
            return new ArrayList<>();
        }
        return ulist.stream().map(vo -> {
            WorkflowUser workflowuser = new WorkflowUser();
            workflowuser.setCode(vo.getUserAccount());
            workflowuser.setName(vo.getUserName());
            return workflowuser;
        }).collect(Collectors.toList());
    }

}
